/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import javax.swing.JDialog;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import soccerscope.db.DatabaseConfig;
import soccerscope.db.DatabaseReader;
import java.sql.*;
import soccerscope.model.SceneSet;
import soccerscope.db.RobocupDatabase;
import javax.swing.JCheckBox;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 * @version 1.0
 */
public class DatabaseConfigDialog extends JDialog {
    private DatabaseConfig dbConfig = DatabaseConfig.getInstance();
    public DatabaseConfigDialog() {
        dbConfig.load();

        try {
            jbInit();

            configInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void configInit() {
        jHostTextField.setText(dbConfig.getSqlHost());
        jDatabaseTextField.setText(dbConfig.getSqlDB());
        jUserTextField.setText(dbConfig.getSqlUser());
        jPasswordTextField.setText(dbConfig.getSqlPassword());
        jDetectPlayingCheckBox.setSelected(dbConfig.isDetectPlaying());
        jDetectTeamsCheckBox.setSelected(dbConfig.isDetectTeams());
    }

    public static void main(String[] args) {
        DatabaseConfigDialog databaseconfigdialog = new DatabaseConfigDialog();
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new DatabaseConfigDialog_jCancelButton_actionAdapter(this));
        jPanel1.setLayout(null);
        jLabel1.setText("Host name");
        jLabel1.setBounds(new Rectangle(29, 41, 67, 14));
        jHostTextField.setText("");
        jHostTextField.setBounds(new Rectangle(132, 38, 192, 20));
        jLabel3.setText("Database name");
        jLabel3.setBounds(new Rectangle(29, 90, 97, 14));
        jDatabaseTextField.setText("");
        jDatabaseTextField.setBounds(new Rectangle(132, 87, 168, 20));
        jLabel4.setText("User name");
        jLabel4.setBounds(new Rectangle(29, 126, 69, 14));
        jUserTextField.setText("");
        jUserTextField.setBounds(new Rectangle(132, 124, 161, 20));
        jLabel5.setText("Password");
        jLabel5.setBounds(new Rectangle(29, 175, 62, 14));
        jPasswordTextField.setText("");
        jPasswordTextField.setBounds(new Rectangle(132, 173, 164, 20));
        this.setTitle("Game settings");
        jClearButton.addActionListener(new DatabaseConfigDialog_jClearButton_actionAdapter(this));
        jSaveButton.addActionListener(new DatabaseConfigDialog_jSaveButton_actionAdapter(this));
        jDetectPlayingCheckBox.setText("Record only after game starts");
        jDetectPlayingCheckBox.setBounds(new Rectangle(27, 248, 249, 22));
        jDetectTeamsCheckBox.setText("Record only after both teams are loaded");
        jDetectTeamsCheckBox.setBounds(new Rectangle(28, 213, 259, 22));
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
        jPanel1.add(jLabel1);
        jPanel1.add(jLabel3);
        jPanel1.add(jLabel4);
        jPanel1.add(jHostTextField);
        jPanel1.add(jUserTextField);
        jPanel1.add(jLabel5);
        jPanel1.add(jPasswordTextField);
        jPanel1.add(jDatabaseTextField);
        jPanel1.add(jDetectTeamsCheckBox);
        jPanel1.add(jDetectPlayingCheckBox);
        jSaveButton.setText("Save");
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
        jClearButton.setText("Clear");
        jPanel2.add(jClearButton);
        jPanel2.add(jSaveButton);
        jPanel2.add(jCancelButton);
        this.setBounds(150, 150, 400, 380);
    }

    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JButton jSaveButton = new JButton();
    JButton jClearButton = new JButton();
    JButton jCancelButton = new JButton();
    JLabel jLabel1 = new JLabel();
    JTextField jHostTextField = new JTextField();
    JLabel jLabel3 = new JLabel();
    JTextField jDatabaseTextField = new JTextField();
    JLabel jLabel4 = new JLabel();
    JTextField jUserTextField = new JTextField();
    JLabel jLabel5 = new JLabel();
    JTextField jPasswordTextField = new JTextField();
    JCheckBox jDetectPlayingCheckBox = new JCheckBox();
    JCheckBox jDetectTeamsCheckBox = new JCheckBox();

    public void jClearButton_actionPerformed(ActionEvent e) {
        jHostTextField.setText("");
        jDatabaseTextField.setText("");
        jUserTextField.setText("");
        jPasswordTextField.setText("");
        jDetectPlayingCheckBox.setSelected(dbConfig.isDetectPlaying());
        jDetectTeamsCheckBox.setSelected(dbConfig.isDetectTeams());
    }

    public void jSaveButton_actionPerformed(ActionEvent e) {
        DatabaseConfig dbConfig = DatabaseConfig.getInstance();
        dbConfig.setSqlHost(jHostTextField.getText());
        dbConfig.setSqlDB(jDatabaseTextField.getText());
        dbConfig.setSqlUser(jUserTextField.getText());
        dbConfig.setSqlPassword(jPasswordTextField.getText());

        dbConfig.setDetectPlaying(jDetectPlayingCheckBox.isSelected()? "true" : "false");
        dbConfig.setDetectTeams(jDetectTeamsCheckBox.isSelected()? "true" : "false");
        dbConfig.store();

        // aktualisiert die DatabaseReader-Instanz
        DatabaseReader dbReader = DatabaseReader.getInstance();
        if (dbReader != null)
            dbReader.restartInstance();

        RobocupDatabase rcDatabase = RobocupDatabase.getInstance();
        if (rcDatabase != null)
           rcDatabase.restartInstance();

        dispose();
    }

    public void jCancelButton_actionPerformed(ActionEvent e) {
        dispose();
    }
}


class DatabaseConfigDialog_jCancelButton_actionAdapter implements ActionListener {
    private DatabaseConfigDialog adaptee;
    DatabaseConfigDialog_jCancelButton_actionAdapter(DatabaseConfigDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jCancelButton_actionPerformed(e);
    }
}


class DatabaseConfigDialog_jSaveButton_actionAdapter implements ActionListener {
    private DatabaseConfigDialog adaptee;
    DatabaseConfigDialog_jSaveButton_actionAdapter(DatabaseConfigDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jSaveButton_actionPerformed(e);
    }
}


class DatabaseConfigDialog_jClearButton_actionAdapter implements ActionListener {
    private DatabaseConfigDialog adaptee;
    DatabaseConfigDialog_jClearButton_actionAdapter(DatabaseConfigDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jClearButton_actionPerformed(e);
    }
}
